<?php
session_start();

if(!isset($_SESSION['uid']))
	die('{"error":2,"error_msg":"User not login."}');

if (isset($_REQUEST['page_size']))
	$pageSize = $_REQUEST['page_size'];
else
	$pageSize = 50;

if(isset($_REQUEST['page']))
	$page = $_REQUEST['page'];
else
	$page = 1;

$pageCount = ceil($_SESSION['friends_count'] / $pageSize);
$page = $page > $pageCount ? $pageCount : $page;
$page = $page < 1 ? 1 : $page;	

include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );

$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
$objFriends = $c->bilateral($_SESSION['uid'], $page, $pageSize);

if($objFriends)
{
	$friends = $objFriends['users'];
	$ouputItems = array();
	foreach($friends as $friend)
	{
		$output = '{"uid":"'.$friend['id'].'",';
		$output = $output.'"name":"'.$friend['screen_name'].'",';
		$output = $output.'"photo":"'.$friend['profile_image_url'].'"}';
		array_push($ouputItems,$output);
	}
	echo "[".implode(",",$ouputItems)."]";
}else{
	echo '{"error":4,"error_msg":"Some error occured when call weibo API"}';
}
?>